﻿using System.IO;
using Acorisoft.FutureGL.MigaDB.Documents;
using KinonekoSoftware.CodariaDB.Documents.Index;
using KinonekoSoftware.CodariaDB.Utils;
using LiteDB;
using ID = KinonekoSoftware.CodariaDB.Utils.ID;
using NewInstanceType = KinonekoSoftware.CodariaDB.InstanceType;
using OldInstanceType = Acorisoft.FutureGL.MigaDB.Interfaces.DocumentType;

namespace KinonekoSoftware.Studio.UpgradeV3.Transformers
{
    /// <summary>
    /// <see cref="InstanceCacheTransformer"/> 转换器用于实现设定之间的转换。
    /// </summary>
    [Pipeline]
    public sealed class InstanceCacheTransformer : Transformer
    {
        private const string OldCollectionName = "idx_doc";
        private const string NewCollectionName = CollectionNames.Cache_InstanceDB;

        public static NewInstanceType GetType(OldInstanceType type)
        {
            return type switch
            {
                OldInstanceType.Skill     => NewInstanceType.Ability,
                OldInstanceType.Item      => NewInstanceType.Item,
                OldInstanceType.Geography => NewInstanceType.Geography,
                OldInstanceType.Creature  => NewInstanceType.Species,
                OldInstanceType.Plant     => NewInstanceType.Plant,
                OldInstanceType.Resource  => NewInstanceType.Item_Resources,
                OldInstanceType.Material  => NewInstanceType.Item_Material,
                OldInstanceType.Ore       => NewInstanceType.Item_Resources,
                OldInstanceType.Country   => NewInstanceType.PoliticalGroups_Country,
                OldInstanceType.Gangbang  => NewInstanceType.Team_Gangs,
                OldInstanceType.Team      => NewInstanceType.Team_Club,
                OldInstanceType.Poison    => NewInstanceType.Item_Item,
                _                         => NewInstanceType.Character,
            };
        }


        public override void Migrate(LiteDatabase oldDB, LiteDatabase newDB, string oldRepoDir, string newRepoDir)
        {
            var newCollection        = newDB.GetCollection<InstanceCache>(NewCollectionName);
            var oldCollection        = oldDB.GetCollection<DocumentCache>(OldCollectionName);
            var newGravatarDirectory = Path.Combine(newRepoDir, "Avatars");
            var oldGravatarDirectory = Path.Combine(oldRepoDir, "Image");

            string avatar = null;

            foreach (var oldCache in oldCollection.FindAll())
            {
                if (oldCache is null)
                {
                    continue;
                }
                
                //
                //
                if (!string.IsNullOrEmpty(oldCache.Avatar))
                {
                    avatar = ID.Get();

                    var oldGravatar = Path.Combine(oldGravatarDirectory, oldCache.Avatar);
                    var newGravatar = Path.Combine(newGravatarDirectory, $"{avatar}.png");

                    if (File.Exists(oldGravatar))
                    {
                        File.Copy(oldGravatar, newGravatar, true);
                    }
                    else
                    {
                        //
                        // 头像文件不存在，那么干脆把这个设置为null
                        avatar = null;
                    }
                }

                var newCache = new InstanceCache
                {
                    Id             = ID.Get(),
                    Name           = oldCache.Name,
                    Intro          = oldCache.Intro,
                    TimeOfCreated  = oldCache.TimeOfCreated,
                    TimeOfModified = oldCache.TimeOfModified,
                    IsDeleted      = oldCache.IsDeleted,
                    IsLocked       = oldCache.IsLocked,
                    Avatar         = avatar,
                    Type           = GetType(oldCache.Type),
                };
                
                //
                //
                newCollection.Insert(newCache);

                //
                //
                avatar = null;
            }
        }
    }
}